package com.nguc.ngucpractice.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nguc.ngucpractice.common.entity.Result;
import com.nguc.ngucpractice.dao.ProductDao;
import com.nguc.ngucpractice.pojo.Product;
import com.nguc.ngucpractice.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * User:曹帅
 * Date:2020/8/2
 * Version:1.0
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductDao productDao;

    @Override
    public Result findProducts(Integer pageNum, Integer pageSize) {
        Result result = new Result();
        pageNum = pageNum == 1 ? 1 : pageNum;
        pageSize = pageSize == 0 ? 5 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        List<Product> products = productDao.selectAll();
        PageInfo<Product> pageInfo = new PageInfo<>(products);
        result.setStatus("0");
        result.setData(pageInfo);
        return result;
    }

    @Override
    @Transactional
    public Result updateStatus(Integer productId, Integer status) throws Exception {
        Result result = new Result();
        Product product = new Product();
        try {
            product = productDao.selectByPrimaryKey(productId);
        } catch (Exception e) {
            throw new Exception("更新失败");
        }
        product.setProductStatus(status);
        productDao.updateByPrimaryKey(product);
        result.setStatus("0");
        result.setData(product);
        return result;
    }

    @Override
    public Result addProduct(Product product) throws Exception {
        Result result = new Result();
        Boolean valid = StringUtils.isEmpty(product.getProductName())
                || StringUtils.isEmpty(product.getPrice())
                || StringUtils.isEmpty(product.getCategoryId())
                || StringUtils.isEmpty(product.getpCategoryId());
        if (valid) {
            throw new Exception("传入参数有误");
        }
        product.setProductStatus(0);
        int insert = productDao.insert(product);
        result.setStatus("0");
        result.setData(insert);
        return result;
    }

    @Override
    public Result updateProduct(Product product) throws Exception {
        Result result = new Result();
        Boolean valid = StringUtils.isEmpty(product.getProductName())
                || StringUtils.isEmpty(product.getPrice())
                || StringUtils.isEmpty(product.getCategoryId())
                || StringUtils.isEmpty(product.getpCategoryId())
                || StringUtils.isEmpty(product.getId());
        if (valid) {
            throw new Exception("传入参数有误");
        }
        Product oldProduct = productDao.selectByPrimaryKey(product.getId());
        if (StringUtils.isEmpty(oldProduct)) {
            throw new Exception("传入商品不存在");
        }
        oldProduct.setPrice(product.getPrice());
        oldProduct.setProductName(product.getProductName());
        oldProduct.setCategoryId(product.getCategoryId());
        oldProduct.setpCategoryId(product.getpCategoryId());
        oldProduct.setImgs(product.getImgs());
        oldProduct.setDetail(product.getDetail());
        oldProduct.setDescription(product.getDescription());
        int i = productDao.updateByPrimaryKey(oldProduct);
        result.setStatus("0");
        result.setData(i);
        return result;
    }

}
